package com.apexsoft.monitor.front.console.task.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.apexsoft.front.common.support.BaseFrontController;
import com.apexsoft.front.common.support.Result;
import com.apexsoft.front.common.utils.annotation.CrudMethod;
import com.apexsoft.monitor.front.console.aop.AopAnnotation;
import com.apexsoft.monitor.front.console.task.service.TaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 任务管理
 */
@Controller("taskController")
@RequestMapping("/console/task")
public class TaskController extends BaseFrontController {
    @Autowired
    @Qualifier("taskService")
    private TaskService taskService = null;


    @CrudMethod
    @AopAnnotation(description = "添加任务管理")
    @RequestMapping(value = "/add")
    public String add(HttpServletRequest request, HttpServletResponse response) throws Exception {
        JSONObject result = taskService.add(request);
        this.printJSON(response, result);
        return null;
    }


    @CrudMethod
    @AopAnnotation(description = "修改任务管理")
    @RequestMapping(value = "/update")
    public String update(HttpServletRequest request, HttpServletResponse response) throws Exception {
        JSONObject result = taskService.update(request);
        this.printJSON(response, result);
        return null;
    }

    @CrudMethod
    @AopAnnotation(description = "删除任务管理")
    @RequestMapping(value = "/delete")
    public String delete(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String hostCode = request.getParameter("hostCode");
        String nodeCode = request.getParameter("nodeCode");
        String taskCode = request.getParameter("taskCode");
        JSONObject result = taskService.delete(hostCode,nodeCode,taskCode);
        this.printJSON(response, result);
        return null;
    }

    //**************************

    @CrudMethod
    @AopAnnotation(description = "保存")
    @RequestMapping(value = "/saveTask")
    public String saveTask(HttpServletRequest request, HttpServletResponse response) throws Exception {
        JSONObject result = taskService.saveTask(request);
        this.printJSON(response, result);
        return null;
    }

    @CrudMethod
    @AopAnnotation(description = "查询任务列表")
    @RequestMapping(value = "/queryTaskList")
    public String getTask(HttpServletRequest request, HttpServletResponse response) throws Exception {
        JSONObject result = taskService.getTask(request);
        JSONArray records = result.getJSONArray(Result.ROWS);
        this.printJSON(response, records==null?new JSONArray():records);
        return null;
    }

    @CrudMethod
    @AopAnnotation(description="跳转模板任务")
    @RequestMapping(value = "/goTemplateTask")
    public String goTemplateTask(HttpServletRequest request, HttpServletResponse response) throws Exception {
        return "console/template/templateTaskDialog";
    }

    @CrudMethod
    @AopAnnotation(description="获取参数")
    @RequestMapping(value = "/getTaskType")
    public String getTaskType(HttpServletRequest request, HttpServletResponse response) throws Exception {
        JSONObject result = taskService.getTask(request);
        this.printJSON(response, result);
        return null;
    }


}

